package com.ziyun.pop.web.modules.goodsCraftPrice.controller;

import com.utility.JsonUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.goodsCraftPrice.dto.GoodsCraftPriceDto;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 报价工艺价格表
 *
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-09-10 10:43:12
 */
@RestController
@RequestMapping("goodsCraftPrice/quoteGoodsCraftPrice")
public class QuoteGoodsCraftPriceController {

    @Value("${erpApi}")
    private String erpApi;

    /**
     * 获取报价商品工艺
     */
    @RequestMapping("/getQuoteGoodsCraftList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getQuoteGoodsCraftList(String attributeOrigin) {

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/getQuoteGoodsCraftList";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("attributeOrigin" ,attributeOrigin);
        String response = HttpUtils.doPost(url, paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /**
     * 获取印刷报价商品工艺价格
     */
    @RequestMapping("/getYinhuaGosodsCraftPriceList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsCraftPriceList() {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/getYinhuaGosodsCraftPriceList";
        String response = HttpUtils.doPost(url, paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 获取印刷商品工艺选项
     */
    @RequestMapping("/getYinshuaGoodsCraftOptionList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getYinshuaGoodsCraftOptionList(Integer attributeId) {

        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/getYinshuaGoodsCraftOptionList";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("attributeId", attributeId);
        paramBodyMap.put("printType", "shangwuyinshua");
        String response = HttpUtils.doPost(url, paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /**
     * 保存印刷商品工艺价格
     */
    @RequestMapping("/saveYinshua")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:save")
    public R saveYinshua(@Valid @RequestBody GoodsCraftPriceDto goodsCraftPriceDto) {
        // 获取当前登录人
        SysUserEntity sysUserEntity = UserUtils.getCurentUser();
        String userName = sysUserEntity.getUsername();
        Long sellerId = sysUserEntity.getSellerId();

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/saveYinshua";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("userName", userName);

        paramBodyMap.put("craftPriceList", JsonUtils.object2String(goodsCraftPriceDto.getCraftPriceList()));


        String response = HttpUtils.doPost(url, paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 根据工艺配价Ids删除
     */
    @RequestMapping("/deleteByIds")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:delete")
    public R deleteByIds(@RequestBody List<Integer> ids) {
        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/deleteByIds";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("ids", JsonUtils.object2String(ids));
        String response = HttpUtils.doPost(url, paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 根据工艺删除
     */
    @RequestMapping("/deleteByAttributeId")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:delete")
    public R deleteByAttributeId(Integer attributeId) {
        Long sellerId = UserUtils.getCurentUser().getSellerId();

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/deleteByAttributeId";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("attributeId", attributeId);
        String response = HttpUtils.doPost(url, paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /*============ 以下为快印商品工艺价格相关信息 ====================*/

    /**
     * 获取快印报价商品工艺价格
     */
    @RequestMapping("/getKuaiyinGosodsCraftPriceList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getKuaiyinGosodsCraftPriceList() {
        Long sellerId = UserUtils.getCurentUser().getSellerId();

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/getKuaiyinGosodsCraftPriceList";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        String response = HttpUtils.doPost(url, paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /**
     * 获取快印商品工艺选项
     */
    @RequestMapping("/getKuaiyinGoodsCraftOptionList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getKuaiyinGoodsCraftOptionList(Integer attributeId) {

        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/getKuaiyinGoodsCraftOptionList";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("attributeId", attributeId);
        paramBodyMap.put("printType", "shumakuaiyin");
        String response = HttpUtils.doPost(url, paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 保存快印商品工艺价格
     */
    @RequestMapping("/saveKuaiyin")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:save")
    public R saveKuaiyin(@Valid @RequestBody GoodsCraftPriceDto goodsCraftPriceDto) {
        // 获取当前登录人
        SysUserEntity sysUserEntity = UserUtils.getCurentUser();
        String userName = sysUserEntity.getUsername();
        Long sellerId = sysUserEntity.getSellerId();

        String url = erpApi + "/goodsCraftPrice/quoteGoodsCraftPrice/saveKuaiyin";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("userName", userName);

        paramBodyMap.put("craftPriceList", JsonUtils.object2String(goodsCraftPriceDto.getCraftPriceList()));

        String response = HttpUtils.doPost(url, paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


}
